library(Amelia)
library(readstata13)
library(foreign)

setwd("C:/Users/fbizz/Dropbox/Party strength/Empirics/Data")

vdem <- read.dta13("ps_data.dta")

vars = c('country_id', 'year','ps', 'e_migdpgro', 'e_migdppcln')
pre.imput = vdem[vars]

cs = 'country_id'
ts = 'year'
m = 10
bounds = matrix(c(3,4,5,
                  0,min(pre.imput$e_migdpgro, na.rm = T),min(pre.imput$e_migdpgro, na.rm = T),
                  1,max(pre.imput$e_migdppcln, na.rm = T),max(pre.imput$e_migdppcln, na.rm = T)),
                ncol = 3, nrow = 3)

post.imput <- amelia(pre.imput, 
                     cs = cs, 
                     ts = ts, 
                     m = m, 
                     bounds = bounds,
                     polytime = 1, 
                     intercs = TRUE, 
                     p2s = 2,
                     empri = 0.1*nrow(pre.imput))

write.amelia(post.imput, file.stem = "ps.imput", format = "dta", impvar = "m", separate = F)





